package pkg

import (
	"context"
	"github.com/qiniu/go-sdk/v7/storagev2/credentials"
	"github.com/qiniu/go-sdk/v7/storagev2/http_client"
	"github.com/qiniu/go-sdk/v7/storagev2/uploader"
	"mime/multipart"
)

func Upload(file *multipart.FileHeader) (string, error) {

	accessKey := "SgoSiHde5GyAJsW3SIiKnlmjruavIKl_HvMtZTbC"
	secretKey := "l05STFRfOdxYahhzaV5TV5XNUDHTfiSIOU_COVCX"
	mac := credentials.NewCredentials(accessKey, secretKey)
	bucket := "2304a123"
	key := file.Filename

	uploadManager := uploader.NewUploadManager(&uploader.UploadManagerOptions{
		Options: http_client.Options{
			Credentials: mac,
		},
	})

	open, err := file.Open()
	if err != nil {
		return "", err
	}
	defer open.Close() // 使用defer在函数退出时关闭文件

	err = uploadManager.UploadReader(context.Background(), open, &uploader.ObjectOptions{
		BucketName: bucket,
		ObjectName: &key,
		CustomVars: map[string]string{
			"name": "github logo",
		},
		FileName: key,
	}, nil)

	return "http://2303ayyq.s3.cn-south-1.qiniucs.com/" + key, err
}
